﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace ServiceHost
{
    [DataContract]
    public class Route
    {
        [DataMember]
        public List<Coordinate> Coordinates { get; set; }

        [DataMember]
        public Town Town { get; set; }

        public bool IsSafe(HotSpot[] hotSpots)
        {
            //No hotspots
            if (hotSpots == null)
            {
                return true;
            }

            //Go through the hotspots and check if route is not too close
            foreach (HotSpot hotSpot in hotSpots)
            {
                if (!IsSafe(hotSpot))
                {
                    return false;
                }
            }

            return true;
        }

        public bool IsSafe(HotSpot hotSpot)
        {
            var result = true;

            //TODO: Check if route is not too close
            Coordinate previousCoordinate = null;
            foreach (var coordinate in Coordinates)
            {
                result = hotSpot.Coordinate.IsTooClose(previousCoordinate, coordinate, Const.HotSpotDistance);
                if (result)
                {
                    return false;
                }

                previousCoordinate = coordinate;
            }
            
            return true;
        }

        public Route()
        {
            this.Coordinates = new List<Coordinate>();
        }

        public Route(string coordinates)
        {
            var list = coordinates.Split(' ');
            this.Coordinates = new List<Coordinate>();

            foreach (var s1 in list)
            {
                if (!string.IsNullOrEmpty(s1))
                {
                    var c = s1.Split(',');
                    this.Coordinates.Add(new Coordinate(Double.Parse(c[0]), Double.Parse(c[1])));
                }
            }
        }
    }
}